<script setup>
import { onMounted, ref } from 'vue';
import axios from 'axios';

// 定义数据容器
const list = ref([]);
// 获取数据函数
const getList = async () => {
  const res = await axios.get('/list');
  console.log(res);
  list.value = res.data;
};
// 组件挂载完毕后发起请求
onMounted(() => {
  getList();
});

// 删除事件处理函数
const delRow = async (id) => {
  console.log(id);
  // 删除
  await axios.delete(`/del?id=${id}`);
  // 重新获取
  getList();
};
</script>

<template>
  <div>
    <el-table :data="list">
      <el-table-column label="ID" prop="id"></el-table-column>
      <el-table-column label="姓名" prop="name"></el-table-column>
      <el-table-column label="籍贯" prop="place"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="{ row }">
          <el-button type="primary" link @click="delRow(row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<style scoped></style>
